package com.jzo2o.foundations.listener;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.jzo2o.foundations.model.domain.ServeSearch;
import com.jzo2o.foundations.service.IRegionServeService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * \* User: chy
 * \* Date: 2025/9/29  17:00
 * \* Description:
 * \
 */
@Component
@Slf4j
public class    ServeSearchToEsListener {

    @Resource
    private RestHighLevelClient esClient;
    @Resource
    private IRegionServeService regionServeService;
    /**
     * 同步mysql中服务到es中
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "serveSearch.add", durable = "true"),
            exchange = @Exchange(name = "serve-exchange"),
            key = "serve-add-es"
    ))
    public void addServeSearch(String msg) {
        log.info("监听到服务新增消息：{}", msg);
        if (!StringUtils.hasText(msg)) {
            return;
        }
        // 1.拿到数据监听到ServeId
        Long serveId = Convert.toLong(msg);
        // 2. 去数据库中查询新增起售状态的服务
        ServeSearch serveSearch = regionServeService.getServeSearchByServeId(serveId);
        if (serveSearch == null) {
            return;
        }
        //3. 添加到es中
        IndexRequest request = new IndexRequest("serve").id(serveSearch.getId() + "");
        request.source(JSONUtil.toJsonStr(serveSearch), XContentType.JSON);
        //4. 发送请求
        try {
            esClient.index(request, RequestOptions.DEFAULT);
        }catch (Exception e){
            log.error("添加服务数据到es异常：", e);
        }
    }
    /**
     * 同步mysql中下架,从es服务删除
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "serveSearch.delete", durable = "true"),//队列名字,持久化
            exchange = @Exchange(name = "serve-exchange"),
            key = "serve-delete-es"
    ))
    public void deleteServeSearch(String msg) {
        log.info("监听到服务删除消息：{}", msg);
        if (!StringUtils.hasText(msg)) {
            return;
        }
        // 1.拿到数据监听到ServeId
        Long serveId = Convert.toLong(msg);
        // 2.去数据库中查询新增起售状态的服务
        ServeSearch serveSearch = regionServeService.getServeSearchByServeId(serveId);
        if (serveSearch == null) {
            return;
        }
        //3. 删除es中的对应数据
        DeleteRequest request = new DeleteRequest("serve").id(serveSearch.getId() + "");
        //4. 删除
        try {
            esClient.delete(request, RequestOptions.DEFAULT);
        }catch (Exception e){
            log.error("删除服务数据到es异常：", e);
        }
    }
}
